package pathways.Database.Implementation;

/**
 * Title:        Pathways
 * Description:  Pure Java version of the Pathways Project.
 * Copyright:    Copyright (c) 2002
 * Company:      Ants Technologies
 * @author Veejay Sani
 * @version 1.0
 */

import java.sql.ResultSet;
import java.sql.SQLException;

import pathways.Database.Interfaces.IDBConnection;
import pathways.util.CUtil;
import pathways.util.Debug.CDebug;

public class CDBUpdate
{

  public CDBUpdate(IDBConnection connection, CDBSelect select)
  {
    conn = connection;
    dbSelect = select;
  }
  
  public boolean UpdateCompound(String cmpdName, String keywords, String ECNO, 
				long cmpdID, String oldCmpdName)
  {
    String sql = "";
    
    //ResultSet s = dbSelect.getCompound(cmpdID);
    
    //try {
    //  if (!s.next())
    //  {
        sql = "UPDATE " + CDBTables.compounds() + " SET ";
        sql = sql + CDBFields.compoundName() + " = '" + cmpdName + "',";
        sql = sql + CDBFields.keywords() + " = '" + keywords + "',";
        sql = sql + CDBFields.ECNO() + " = '" + ECNO + "'";
        sql = sql + " WHERE ";
        sql = sql + CDBFields.compoundID() + " = " + cmpdID;
        sql = sql + " AND ";
	sql = sql + CDBFields.compoundName() + " = '" + oldCmpdName + "'";
	
        int count = conn.runUpdate(sql);
        
        return (count > 0);
    //  }
    //} catch (SQLException e) {
    //  CDebug.println(this, "UpdateReaction() - Exception: " + e.toString());
    //}
    //return true;
  }
  
  public long newCompound(String cmpdName, String keywords, String ECNO)  
  {
    String sql = "";
    long id = getNewCompoundID("User");  // put user later ???
    
    if (!(id >0))
    {
      CDebug.println(this, "newCompound() -- Big trouble in little China. Could not generate an ID for compound.");
      return 0;  
    }
    
    sql = "INSERT INTO " + CDBTables.compounds() + "(";
    sql = sql + CDBFields.compoundID() + ",";
    sql = sql + CDBFields.compoundName() + ",";
    sql = sql + CDBFields.keywords() + ",";
    sql = sql + CDBFields.ECNO();
    sql = sql + ") VALUES (";
    sql = sql + id  + ",";
    sql = sql + "'" + cmpdName + "',";
    sql = sql + "'" + keywords + "',";
    sql = sql + "'" + ECNO + "')";
    
    int count = conn.runUpdate(sql);
        
    return id;
  }
  
  public boolean UpdateEnzymeForReaction(long rxnID, long enzymeID)
  {
    String sql = "";
    
    sql = "UPDATE " + CDBTables.reactions() + " SET ";
    sql = sql + CDBFields.enzymeID() + " = " + enzymeID;
    sql = sql + " WHERE ";
    sql = sql + CDBFields.reactionID() + " = " + rxnID;
    
    int count = conn.runUpdate(sql);
    
    return (count > 0); 
  }
  
  public boolean UpdateSubForReaction(long rxnID, long subID, long oldSubID)
  {
    String sql = "";
    
    sql = "UPDATE " + CDBTables.reactions() + " SET ";
    sql = sql + CDBFields.substrateID() + " = " + subID;
    sql = sql + " WHERE ";
    sql = sql + CDBFields.reactionID() + " = " + rxnID;
    sql = sql + " AND ";
    sql = sql + CDBFields.substrateID() + " = " + oldSubID;
    
    int count = conn.runUpdate(sql);
    
    return (count > 0);
  }

  public boolean UpdateProdForReaction(long rxnID, long prodID, long oldProdID)
  {
    String sql = "";
    
    sql = "UPDATE " + CDBTables.reactions() + " SET ";
    sql = sql + CDBFields.productID() + " = " + prodID;
    sql = sql + " WHERE ";
    sql = sql + CDBFields.reactionID() + " = " + rxnID;
    sql = sql + " AND ";
    sql = sql + CDBFields.productID() + " = " + oldProdID;
    
    int count = conn.runUpdate(sql);
    
    return (count > 0);
  }
  
  public boolean UpdateReaction(long autoNumber, long subID, long enzymeID, long productID)
  {
    String sql = "";
    
    //System.out.println(subID + ", " + enzymeID + ", " + productID);
     
    //ResultSet s = dbSelect.getReaction(subID,enzymeID,productID);
    
    //try{
      //if (!s.next())
      //{
        sql = "UPDATE " + CDBTables.reactions() + " SET ";
        sql = sql + CDBFields.substrateID() + " = " + subID + ",";
        sql = sql + CDBFields.enzymeID() + " = " + enzymeID + ",";
        sql = sql + CDBFields.productID() + " = " + productID;
        sql = sql + " WHERE ";
        sql = sql + CDBFields.autoNumber() + " = " + autoNumber;
        
        int count = conn.runUpdate(sql);
        
        return (count > 0);
      //}
    //} catch (SQLException e) {
    //  CDebug.println(this, "UpdateReaction() - Exception: " + e.toString());
    //}
    
    //return true;
  }
  
  public long newReaction(long subID, long enzymeID, long productID)
  {
    String sql = "";
    long id = getNewReactionID("User");  // set the user id later ???
    
    if (!(id >0))
    {
      CDebug.println(this, "newReaction() -- Big trouble in little China. Could not generate an ID for reaction.");
      return 0;  
    }
     
    sql = "INSERT INTO " + CDBTables.reactions() + "(";
    sql = sql + CDBFields.reactionID() + ",";
    sql = sql + CDBFields.substrateID() + ",";
    sql = sql + CDBFields.enzymeID() + ",";
    sql = sql + CDBFields.productID();
    sql = sql + ") VALUES (";
    sql = sql + id + ",";
    sql = sql + subID + ",";
    sql = sql + enzymeID + ",";
    sql = sql + productID + ")";
    
    conn.runUpdate(sql);
    
    return id;
  }
  
  private long getNewReactionID(String user)
  {
    String sql = "";
    java.util.Date d = new java.util.Date();
    long time = d.getTime();
    long ID = 0;
    
    sql = "INSERT INTO " + CDBTables.getReactionID() + "(";
    sql = sql + CDBFields.temp() + ",";
    sql = sql + CDBFields.userName();
    sql = sql + " ) VALUES ( ";
    sql = sql + time + ",";
    sql = sql + "'" + user + "')";
    
    conn.runUpdate(sql);
    
    sql = "SELECT " + CDBFields.reactionID() + " FROM ";
    sql = sql + CDBTables.getReactionID();
    sql = sql + " WHERE ";
    sql = sql + CDBFields.userName() + " = '" + user + "'";
    sql = sql + " AND ";
    sql = sql + CDBFields.temp() + " = " + time;
    
    ResultSet r = conn.runQuery(sql);
    
    try{
      r.next();
      ID = r.getLong(1);
    } catch(SQLException e) {
      CDebug.println(this,"getNewReactionID() - Exception: " + e.toString());
      ID = 0; 
    }
    
    return ID;
  }

  private long getNewCompoundID(String user)
  {
    String sql = "";
    java.util.Date d = new java.util.Date();
    long time = d.getTime();
    long ID = 0;
    
    sql = "INSERT INTO " + CDBTables.getCompoundID() + "(";
    sql = sql + CDBFields.temp() + ",";
    sql = sql + CDBFields.userName();
    sql = sql + " ) VALUES ( ";
    sql = sql + time + ",";
    sql = sql + "'" + user + "')";
    
    conn.runUpdate(sql);
    
    sql = "SELECT " + CDBFields.compoundID() + " FROM ";
    sql = sql + CDBTables.getCompoundID();
    sql = sql + " WHERE ";
    sql = sql + CDBFields.userName() + " = '" + user + "'";
    sql = sql + " AND ";
    sql = sql + CDBFields.temp() + " = " + time;
    
    ResultSet r = conn.runQuery(sql);
    
    try{
      r.next();
      ID = r.getLong(1);
    } catch(SQLException e) {
      CDebug.println(this,"getNewCompoundID() - Exception: " + e.toString());
      ID = 0; 
    }
    
    return ID;
  }
  
  public boolean newReaction(long subID, long enzymeID, long productID, long rxnID)
  {
    String sql = "";
     
    sql = "INSERT INTO " + CDBTables.reactions() + "(";
    sql = sql + CDBFields.reactionID() + ",";
    sql = sql + CDBFields.substrateID() + ",";
    sql = sql + CDBFields.enzymeID() + ",";
    sql = sql + CDBFields.productID();
    sql = sql + ") VALUES (";
    sql = sql + rxnID + ",";
    sql = sql + subID + ",";
    sql = sql + enzymeID + ",";
    sql = sql + productID + ")";
    
    int count = conn.runUpdate(sql);
        
    return (count > 0);
  }
  
  public boolean deleteReaction(long rxnID, long subID, long prodID)
  {
    String sql = "";
    
    sql = "DELETE FROM " + CDBTables.reactions(); 
    sql = sql + " WHERE ";
    sql = sql + CDBFields.reactionID() + " = " + rxnID;
    sql = sql + " AND ";
    sql = sql + CDBFields.substrateID() + " = " + subID;
    sql = sql + " AND ";
    sql = sql + CDBFields.productID() + " = " + prodID;
    
    int count = conn.runUpdate(sql);
    
    return (count > 0);
  }
  
  public boolean addCoSubstrate(long rxnID, long cmpdID, int parts)
  {
    String sql = "";
    
    sql = "INSERT INTO " + CDBTables.coSubstrates() + "(";
    sql = sql + CDBFields.compoundID() + ",";
    sql = sql + CDBFields.reactionID() + ",";
    sql = sql + CDBFields.parts(); 
    sql = sql + " ) VALUES ( ";
    sql = sql + cmpdID + ",";
    sql = sql + rxnID + ",";
    sql = sql + parts + ")";
    
    int count = conn.runUpdate(sql);
    
    return (count > 0);
  }

  public boolean addCoProduct(long rxnID, long cmpdID, int parts)
  {
    String sql = "";
    
    sql = "INSERT INTO " + CDBTables.coProducts() + "(";
    sql = sql + CDBFields.compoundID() + ",";
    sql = sql + CDBFields.reactionID() + ",";
    sql = sql + CDBFields.parts(); 
    sql = sql + " ) VALUES ( ";
    sql = sql + cmpdID + ",";
    sql = sql + rxnID + ",";
    sql = sql + parts + ")";
    
    int count = conn.runUpdate(sql);
    
    return (count > 0);
  }
  
  public boolean removeCoSubstrate(long rxnID, long cmpdID)
  {
    String sql = "";
    
    sql = "DELETE FROM " + CDBTables.coSubstrates();
    sql = sql + " WHERE ";
    sql = sql + CDBFields.reactionID() + " = " + rxnID;
    sql = sql + " AND ";
    sql = sql + CDBFields.compoundID() + " = " + cmpdID;
    
    int count = conn.runUpdate(sql);
    
    return (count > 0);     
  }

  public boolean removeCoProduct(long rxnID, long cmpdID)
  {
    String sql = "";
    
    sql = "DELETE FROM " + CDBTables.coProducts();
    sql = sql + " WHERE ";
    sql = sql + CDBFields.reactionID() + " = " + rxnID;
    sql = sql + " AND ";
    sql = sql + CDBFields.compoundID() + " = " + cmpdID;
    
    int count = conn.runUpdate(sql);
    
    return (count > 0);     
  }
  
  public boolean newTrace(long rxnID, long subID, long prodID, int from, int to)
  {
    String sql = "";
    
    sql = "INSERT INTO " + CDBTables.atomTraces() + "(";
    sql = sql + CDBFields.reactionID() + ",";
    sql = sql + CDBFields.substrateID() + ",";
    sql = sql + CDBFields.productID() + ",";
    sql = sql + CDBFields.from() + ",";
    sql = sql + CDBFields.to();
    sql = sql + ") VALUES (";
    sql = sql + rxnID + ",";
    sql = sql + subID + ",";
    sql = sql + prodID + ",";
    sql = sql + from + ",";
    sql = sql + to + ")";
    
    int count = conn.runUpdate(sql);
    
    return (count > 0);
  }
  
  public boolean deleteTraces(long rxnID)
  {
    String sql = "";
    
    sql = "DELETE FROM " + CDBTables.atomTraces();
    sql = sql + " WHERE ";
    sql = sql + CDBFields.reactionID() + " = " + rxnID;
    
    int count = conn.runUpdate(sql);
    
    return (count > 0);  
  }
  
  public long newInstantiatedData(int fieldID, String value, long instRxnID, int userID, String reference)
  {
    String sql = "";
    long id = 0;
    
    sql = "INSERT INTO " + CDBTables.instantiatedData() + "(";
    sql = sql + CDBFields.fieldID() + ",";
    sql = sql + CDBFields.value() + ",";
    sql = sql + CDBFields.instantiatedRxnID() + ",";
    sql = sql + CDBFields.userID() + ",";
    sql = sql + CDBFields.reference();
    sql = sql + " ) VALUES ( ";
    sql = sql + fieldID + ",";
    sql = sql + "'" + value + "',";
    sql = sql + instRxnID + ",";
    sql = sql + userID + ",";
    sql = sql + "'" + reference + "')";
    
    int count = conn.runUpdate(sql);
    
    if (count > 0)
    {
      id = dbSelect.getInstantiatedDataID(fieldID,value,instRxnID,userID,reference);     
    }
    
    return id; 
  }
  
  public boolean updateInstantiatedData(long dataID, int fieldID, String value, String reference)
  {
    String sql = "";
    
    sql = "UPDATE " + CDBTables.instantiatedData() + " SET ";
    sql = sql + CDBFields.fieldID() + " = " + fieldID + ",";
    sql = sql + CDBFields.value() + " = '" + value + "',";
    sql = sql + CDBFields.reference() + " = '" + reference + "'";
    sql = sql + " WHERE ";
    sql = sql + CDBFields.dataID() + " = " + dataID;
    
    int count = conn.runUpdate(sql);
    
    return (count > 0);
  }
  
  public boolean deleteInstantiatedData(long dataID)
  {
    String sql = "";
    
    sql = "DELETE FROM " + CDBTables.instantiatedData();
    sql = sql + " WHERE ";
    sql = sql + CDBFields.dataID() + " = " + dataID;
    
    int count = conn.runUpdate(sql);
    
    return (count > 0);  
  }
  
  public int newSpace(int tissueID, int organID, int cellID, int subCellSubID, int subCellProdID)
  {
    String sql = "";
    int id = dbSelect.getSpace(tissueID, organID, cellID, subCellSubID, subCellProdID);
    
    if (id <= 0)
    {
      sql = "INSERT INTO " + CDBTables.space() + "(";
      sql = sql + CDBFields.tissueID() + ",";
      sql = sql + CDBFields.organID() + ",";
      sql = sql + CDBFields.cellID() + ",";
      sql = sql + CDBFields.subCellSubID() + ",";
      sql = sql + CDBFields.subCellProdID();
      sql = sql + " ) VALUES ( ";
      sql = sql + tissueID + ",";
      sql = sql + organID + ",";
      sql = sql + cellID + ",";
      sql = sql + subCellSubID + ",";
      sql = sql + subCellProdID + ")";
      
      int count = conn.runUpdate(sql);
    
      id = dbSelect.getSpace(tissueID, organID, cellID, subCellSubID, subCellProdID);
    }
    
    return id;
  }
  
  public int newInstantiatedRxn(int spaceID, int reactionID, int taxID)
  {
    String sql = "";
    int id = 0;
    String time = CUtil.getCurrentTime();
    
    sql = "INSERT INTO " + CDBTables.instantiatedReactions() + "(";
    sql = sql + CDBFields.spaceID() + ",";
    sql = sql + CDBFields.reactionID() + ",";
    sql = sql + CDBFields.taxID() + ",";
    sql = sql + CDBFields.date();
    sql = sql + " ) VALUES ( ";
    sql = sql + spaceID + ",";
    sql = sql + reactionID + ",";
    sql = sql + taxID + ","; 
    sql = sql + "'" + time + "')";
    
    int count = conn.runUpdate(sql);
    
    if (count > 0)
    {
      id = dbSelect.getInstantiationID(spaceID, reactionID, taxID,time);  
    }
    
    return id;
  }
  
  public boolean updateInstantiation(int instRxnID, int spaceID, int taxID)
  {
    String sql = "";
    
    sql = "UPDATE " + CDBTables.instantiatedReactions() + " SET ";
    sql = sql + CDBFields.spaceID() + " = " + spaceID + ",";
    sql = sql + CDBFields.taxID() + " = " + taxID;
    sql = sql + " WHERE ";
    sql = sql + CDBFields.instantiatedRxnID() + " = " + instRxnID;
    
    int count = conn.runUpdate(sql);
    
    return (count > 0);  
  }
  
  public boolean newSavedPathwayItem(int pathwayID, int rxnID, 
         int subID, int prodID, String subsCoordinates, int subsCoordType,
         String prodCoordinates, int prodCoordType, int fromPoint, 
         int toPoint, int instRxnID)
  {
    String sql = "";
    
    sql = "INSERT INTO " + CDBTables.pathway() + "(";
    sql = sql + CDBFields.pathwayID() + ",";
    sql = sql + CDBFields.reactionID() + ",";
    sql = sql + CDBFields.substrateID() + ",";
    sql = sql + CDBFields.productID() + ",";
    sql = sql + CDBFields.substrateCoordinates() + ",";
    sql = sql + CDBFields.substrateCoordinateType() + ",";
    sql = sql + CDBFields.productCoordinates() + ",";
    sql = sql + CDBFields.productCoordinateType() + ",";
    sql = sql + CDBFields.fromPoint() + ",";
    sql = sql + CDBFields.toPoint() + ",";
    sql = sql + CDBFields.instantiatedRxnID();
    sql = sql + ") VALUES (";
    sql = sql + pathwayID + ",";
    sql = sql + rxnID + ",";
    sql = sql + subID + ",";
    sql = sql + prodID + ",";
    sql = sql + "'" + subsCoordinates + "',";
    sql = sql + subsCoordType + ",";
    sql = sql + "'" + prodCoordinates + "',";
    sql = sql + prodCoordType + ",";
    sql = sql + fromPoint + ",";
    sql = sql + toPoint + ",";
    sql = sql + instRxnID;
    sql = sql + ")";
    
    int count = conn.runUpdate(sql);
    
    return (count > 0);
  }
  
  public int newPathway(int userID, String pathwayName, String date)
  {
    String sql = "";
    int id = 0;
    
    sql = "INSERT INTO " + CDBTables.pathwayID() + "(";
    sql = sql + CDBFields.userID() + ",";
    sql = sql + CDBFields.pathwayName() + ",";
    sql = sql + CDBFields.date();
    sql = sql + ") VALUES (";
    sql = sql + userID + ",";
    sql = sql + "'" + pathwayName + "',";
    sql = sql + "'" + date + "')";
    
    int count = conn.runUpdate(sql);
    
    if (count > 0)
    {
      id = dbSelect.getPathwayID(userID,pathwayName); 
    }
    
    return id;   
  }
  
  public boolean newPathwayConnectionPointBranch(int pathwayID, int subID, 
         int connectionPoint, int rxnID)
  {
    String sql = "";
    
    sql = "INSERT INTO " + CDBTables.pathwayConnectionBranches() + "(";
    sql = sql + CDBFields.pathwayID() + ",";
    sql = sql + CDBFields.substrateID() + ",";
    sql = sql + CDBFields.connectionPoint() + ",";
    sql = sql + CDBFields.reactionID();
    sql = sql + ") VALUES (";
    sql = sql + pathwayID + ",";
    sql = sql + subID + ",";
    sql = sql + connectionPoint + ",";
    sql = sql + rxnID + ")";
    
    int count = conn.runUpdate(sql);
    
    return (count > 0);
  } 
  
  public boolean newPathwaySubstrateAsEnzyme(int pathwayID, int subID,
         int rxnID, int fromPoint, int toPoint)
  {
    String sql = "";
    
    sql = "INSERT INTO " + CDBTables.pathwaySubstratesAsEnzymes() + "(";
    sql = sql + CDBFields.pathwayID() + ",";
    sql = sql + CDBFields.substrateID() + ",";
    sql = sql + CDBFields.reactionID() + ",";
    sql = sql + CDBFields.fromPoint() + ",";
    sql = sql + CDBFields.toPoint();
    sql = sql + ") VALUES (";
    sql = sql + pathwayID + ",";
    sql = sql + subID + ",";
    sql = sql + rxnID + ",";
    sql = sql + fromPoint + ",";
    sql = sql + toPoint + ")";
    
    int count = conn.runUpdate(sql);
    
    return (count > 0);
  }
  
  public int newInhibitor(String strInhibitor, int rxnID)
  {
    String sql = "";
    int id = dbSelect.getInhibitor(strInhibitor);
    
    if (id <= 0)
    {
      sql = "INSERT INTO " + CDBTables.inhibitors() + "(";
      sql = sql + CDBFields.inhibitor() + ",";
      sql = sql + CDBFields.reactionID();
      sql = sql + ") VALUES (";
      sql = sql + "'" + strInhibitor + "',";
      sql = sql + rxnID + ")";
      
      conn.runUpdate(sql);
        
      id = dbSelect.getInhibitor(strInhibitor);
    }
    
    return id;
  }
  
  public boolean updateInhibitor(String strInhibitor, String strNewValue)
  {
    String sql = "";
    
    sql = "UPDATE " + CDBTables.inhibitors() + " SET ";
    sql = sql + CDBFields.inhibitor() + " = '" + strNewValue + "'";
    sql = sql + " WHERE ";
    sql = sql + CDBFields.inhibitor() + " = '" + strInhibitor + "'";
    
    int count = conn.runUpdate(sql);
    
    return (count > 0);
  }
  
  public boolean deleteInhibitor(int id)
  {
    String sql = "";
    
    sql = "DELETE FROM " + CDBTables.inhibitors();
    sql = sql + " WHERE ";
    sql = sql + CDBFields.autoNumber() + " = " + id;
    
    int count = conn.runUpdate(sql);
    
    return (count > 0);
  }
  
  public boolean newSynonymn(int master_CmpdID, int slave_CmpdID)
  {
    // Pre: Start a Database Transaction before calling this function
    
    /* updates the following tables
      v2_Compound_Table
      v2_Reaction_Table
      v2_CoSubstrate_Table
      v2_CoProduct_Table
      tblPathway
      tblPathwayConnectionBranches
      tblPathwaySubstrateAsEnzyme
      tblAtomTraces
      */
      
      String sql = "";
      int count = 0;
      
      // v2_Compound_Table
      sql = getSQLFor_newSynonymn(CDBTables.compounds(),CDBFields.compoundID(),master_CmpdID, slave_CmpdID);
      count = conn.runUpdate(sql);
      
      // v2_Reactions_Tables
      sql = getSQLFor_newSynonymn(CDBTables.reactions(), CDBFields.substrateID(),master_CmpdID, slave_CmpdID);
      count = conn.runUpdate(sql);
      
      sql = getSQLFor_newSynonymn(CDBTables.reactions(), CDBFields.enzymeID(),master_CmpdID, slave_CmpdID);
      count = conn.runUpdate(sql);
      
      sql = getSQLFor_newSynonymn(CDBTables.reactions(), CDBFields.productID(),master_CmpdID, slave_CmpdID);
      count = conn.runUpdate(sql);
      
      // v2_CoProds
      sql = getSQLFor_newSynonymn(CDBTables.coProducts(), CDBFields.compoundID(),master_CmpdID, slave_CmpdID);
      count = conn.runUpdate(sql);
      
      // v2_CoSubs
      sql = getSQLFor_newSynonymn(CDBTables.coSubstrates(), CDBFields.compoundID(),master_CmpdID, slave_CmpdID);
      count = conn.runUpdate(sql);
      
      // tblPathway
      sql = getSQLFor_newSynonymn(CDBTables.pathway(), CDBFields.substrateID(),master_CmpdID, slave_CmpdID);
      count = conn.runUpdate(sql);
      
      sql = getSQLFor_newSynonymn(CDBTables.pathway(), CDBFields.productID(),master_CmpdID, slave_CmpdID);
      count = conn.runUpdate(sql);
      
      // tblPathwayConnectionBranches
      sql = getSQLFor_newSynonymn(CDBTables.pathwayConnectionBranches(), CDBFields.substrateID(),master_CmpdID, slave_CmpdID);
      count = conn.runUpdate(sql);
      
      // tblPathwaySubstrateAsEnzyme
      sql = getSQLFor_newSynonymn(CDBTables.pathwaySubstratesAsEnzymes(), CDBFields.substrateID(),master_CmpdID, slave_CmpdID);
      count = conn.runUpdate(sql);
      
      // tblAtomTraces
      sql = getSQLFor_newSynonymn(CDBTables.atomTraces(), CDBFields.substrateID(),master_CmpdID, slave_CmpdID);
      count = conn.runUpdate(sql);
      
      sql = getSQLFor_newSynonymn(CDBTables.atomTraces(), CDBFields.productID(), master_CmpdID, slave_CmpdID);
      count = conn.runUpdate(sql);
      
      return true;
  }
  
  private String getSQLFor_newSynonymn(String tbl, String field, 
					int masterID, int slaveID)
  {
    String sql = "";
    
    sql = "UPDATE " + tbl + " SET ";
    sql = sql + field + " = " + masterID;
    sql = sql + " WHERE ";
    sql = sql + field + " = " + slaveID;
    
    return sql;
  }
  
  private IDBConnection conn;
  private CDBSelect dbSelect;
}